"""
-*- coding: utf-8 -*-
@Time : 2022/5/22 21:08
@Author : XGT-Python
"""
import requests
from lxml import etree
import csv

with open('17172.csv', 'w', encoding='utf-8', newline='') as fw:
    csv_writer = csv.writer(fw)
    csv_writer.writerow(['游戏名称','投票数量','测试状态'])

def get_html(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
    }
    return requests.get(url, headers=headers).text


def parse_text(text):
    dom = etree.HTML(text)
    return dom


if __name__ == '__main__':
    for num in range(1,38):
        url = f'http://top.17173.com/list-2-0-0-0-0-0-0-0-0-0-{num}.html'
        resp = get_html(url)
        dom = parse_text(resp)
        div_List = dom.xpath('//div[@class="mod-bd"]/ul/li')
        for div in div_List:
            game_name = div.xpath('./div/div[2]/div/a/text()')[0]
            vote = div.xpath('./div/div[3]/text()')[0].strip()
            test_status = div.xpath('./div/div[5]/text()')[0].strip()
            print(game_name,vote,test_status,)
            with open('17172.csv','a',encoding='utf-8',) as fw:
                csv_writer = csv.writer(fw)
                csv_writer.writerow([game_name,vote,test_status])

